from typing import Dict, Any
from langgraph.graph import StateGraph
from pydantic import BaseModel, Field, ValidationError
from intelli_port.integrations.checkin import checkin_ticket


class ServiceCheckIn(BaseModel):
    type: str = Field(default="check_in")
    accepted: bool
    request_created: bool


def build_checkin_subgraph():
    g = StateGraph(dict)

    def exec_checkin(state: Dict[str, Any]) -> Dict[str, Any]:
        slots = state.get("slots") or {}
        res = checkin_ticket({"ticket_no": slots.get("ticket_no")})
        try:
            svc = ServiceCheckIn.model_validate({"type": "check_in", **res}).model_dump()
        except ValidationError:
            svc = {"type": "check_in", "accepted": False, "request_created": False}
        return {**state, "service": svc}

    g.add_node("checkin", exec_checkin)
    g.set_entry_point("checkin")
    return g.compile()